﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class DanhSachMonDAO
    {
        // Operation...
        public static bool ThemMon(DanhSachMonDTO mon)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Insert into DANHSACHMON (Ban, Mon, SoLuong, GhiChu) values (@Ban, @Mon, @SoLuong, @GhiChu)";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@Ban", OleDbType.Integer);
                thamso.Value = mon.Ban.MaBan;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Mon", OleDbType.Integer);
                thamso.Value = mon.Mon.MaMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@SoLuong", OleDbType.Integer);
                thamso.Value = mon.SoLuong;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@GhiChu", OleDbType.VarChar);
                thamso.Value = mon.GhiChu;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool TraMon(DanhSachMonDTO mon)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Delete From DANHSACHMON where Mon = @Mon and Ban = @Ban";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@Mon", OleDbType.Integer);
                thamso.Value = mon.Mon.MaMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Ban", OleDbType.Integer);
                thamso.Value = mon.Ban.MaBan;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool XoaDanhSachMon(BanDTO ban)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Delete From DANHSACHMON where Ban = @Ban";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso = new OleDbParameter("@Ban", OleDbType.Integer);
                thamso.Value = ban.MaBan;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static List<DanhSachMonDTO> LayDanhSachMon(BanDTO ban)
        {
            List<DanhSachMonDTO> dsMon = new List<DanhSachMonDTO>();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select Mon, SoLuong, GhiChu From DANHSACHMON Where Ban = @Ban";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso = new OleDbParameter("@Ban", OleDbType.Integer);
                thamso.Value = ban.MaBan;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    DanhSachMonDTO temp = new DanhSachMonDTO();
                    temp.Mon.MaMon = bodoc.GetInt32(0);
                    if (!bodoc.IsDBNull(1))
                        temp.SoLuong = bodoc.GetInt16(1);
                    if (!bodoc.IsDBNull(2))
                        temp.GhiChu = bodoc.GetString(2);

                    dsMon.Add(temp);
                }
            }
            catch (Exception ex)
            {
                dsMon = new List<DanhSachMonDTO>();
            }

            return dsMon;
        }
    }
}
